/*
 * 文件名：MemCache.java
 * 版权：Copyright by www.huawei.com
 * 描述：
 * 修改人：yangl
 * 修改时间：2016-1-10
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.yangl.redis;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.springframework.data.redis.core.RedisTemplate;

public class RedisCacheUtil {
//	private static Logger log = LoggerFactory.getLogger(MemCache.class);   
	  
    private Set<String> keySet = new HashSet<String>();   
    private final String name;   
    private final int expire;   
    private final RedisTemplate<Object, Object> redisTemplate;
  
    public RedisCacheUtil(String name, int expire, RedisTemplate<Object, Object> redisTemplate)   
    {   
        this.name = name;   
        this.expire = expire;   
        this.redisTemplate = redisTemplate;   
    }   
  
    public Object get(String key)   
    {   
        Object value = null;   
        key = this.getKey(key);   
        value = redisTemplate.opsForValue().get(key);   
        return value;   
    }   
  
    public void put(String key, Object value)   
    {   
        if (value == null)   
            return;   
        
        key = this.getKey(key);
        redisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS);
        keySet.add(key);   
    }   
       
       
    public void clear()   
    {   
        for (String key : keySet)   
        {   
            	redisTemplate.delete(this.getKey(key));   
        }   
    }   
       
       
    public void delete(String key)   
    {   
        key = this.getKey(key);   
        redisTemplate.delete(key);   
         
    }   
       
       
    private String getKey(String key)   
    {   
        return name + "_" + key;   
    }
}
